package com.leyou.page.service;

import com.leyou.item.pojo.*;
import com.leyou.page.client.BrandClient;
import com.leyou.page.client.CategoryClient;
import com.leyou.page.client.GoodsClient;
import com.leyou.page.client.SpecificationClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/10/13
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;

    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private BrandClient brandClient;
    @Autowired
    private SpecificationClient specificationClient;
    public Map<String,Object> loadModel(Long id) {
        Map<String, Object> model = new HashMap<>();
        try {
            CountDownLatch count = new CountDownLatch(5);

            ExecutorService pool = Executors.newFixedThreadPool(5);
            //先查询spu的信息
            Spu spu= goodsClient.querySpuBySpuId(id);
            model.put("spu", spu);
            pool.execute(()->{
                //查询sku的信息
                model.put("skus", goodsClient.querySkuBySpuId(id));
                //查询spudetail
                SpuDetail spuDetail = goodsClient.querySpuDetailById(id);
                model.put("spuDetail", spuDetail);
                count.countDown();
            });

            pool.execute(()->{
                //查询分类信息
                List<Long>ids= Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
                List<String> categoriesName = categoryClient.queryNamesByIds(ids);
                ArrayList<Category> categories = new ArrayList<>();
                for (int i = 0; i < ids.size(); i++) {
                    Category category = new Category();
                    category.setName(categoriesName.get(i));
                    category.setId(category.getId());
                    categories.add(category);
                }
                model.put("categories", categories);
                count.countDown();
            });
            pool.execute(()->{
                //查询品牌信息
                List<Brand> brands = brandClient.queryBrandByIds(Arrays.asList(spu.getBrandId()));
                model.put("brand", brands.get(0));
                count.countDown();
            });

            pool.execute(()->{
                //查询分类的group和params信息
                List<SpecGroup> groups =specificationClient.querySpecsByCid(spu.getCid3());
                model.put("groups", groups);
                count.countDown();
            });

            pool.execute(()->{
                // 查询商品分类下的特有规格参数,供detile用
                List<SpecParam> params = this.specificationClient.querySpecParams(null, spu.getCid3(), null, false);
                // 处理成id:name格式的键值对
                Map<Long, String> paramMap = new HashMap<>();
                params.forEach(param-> paramMap.put(param.getId(), param.getName()));
                model.put("paramMap", paramMap);
                count.countDown();
            });
            count.await();
            return model;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
